package root.electronclass.electronclass.grade.attendance

import android.content.res.ColorStateList
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.forEachIndexed
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.mvp.base.data
import root.electronclass.electronclass.mvp.base.isSuccessful
import root.electronclass.electronclass.mvp.rest.ClassApi
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.FragmentClassAttendanceStatBinding
import root.electronclass.electronclass.databinding.FragmentClassAttendanceStatItemBinding
import root.electronclass.electronclass.ext.*
import root.electronclass.electronclass.grade.model.ClassAttendanceReport
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 历史考勤统计
 * @author Lokiy
 * @date 2023/3/22 23:30
 */
class ClassAttendanceStatisticalFragment : ViewBindingFragment<FragmentClassAttendanceStatBinding>() {

    private var type = 1 //1-本周 2-本月 3-3个月 4-本学期

    private val itemsAdapter = BasicAdapter<ClassAttendanceReport, FragmentClassAttendanceStatItemBinding>(onLoadMore = { getData() }) { holder, item ->
        holder.binding.root.setBackgroundResource(if (holder.adapterPosition % 2 == 0) 0 else R.drawable.bg_306eff_8)
        holder.binding.root.backgroundTintList = ColorStateList.valueOf(0x15306eff)
        holder.binding.itemPosition.text = item.rank.toString()
        holder.binding.itemName.text = item.name
        holder.binding.itemNo.text = item.userNo
        holder.binding.itemAttendanceToday.text = item.today
        holder.binding.itemAttendanceWeek.text = if (item.week == 0) "暂无数据" else "%d%%".format(item.week)
        holder.binding.itemAttendanceMonth.text = if (item.month == 0) "暂无数据" else "%d%%".format(item.month)
        holder.binding.itemAttendance3Month.text = if (item.threeMonth == 0) "暂无数据" else "%d%%".format(item.threeMonth)
        holder.binding.itemAttendanceSemester.text = if (item.term == 0) "暂无数据" else "%d%%".format(item.term)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val tabs = arrayOf("本周", "本月", "近3个月", "本学期")
        binding.tabLayout.tabs(tabs) {
            type = tabs.indexOf(it) + 1
            getData()
        }
        val padding = dp2px(24.dp).toInt()
        binding.studentRecyclerTitle.root.apply {
            val names = resources.getStringArray(R.array.class_attendance_titles)
            forEachIndexed { index, it ->
                val view1: View
                if (it is TextView) {
                    view1 = it
                } else {
                    it as ViewGroup
                    view1 = it.getChildAt(0)
                    view1 as TextView
                }
                view1.setTextColor(0xFFFFFFFF.toInt())
                view1.text = names[index]
            }
            setBackgroundResource(R.drawable.bg_306eff_8)
            updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = padding
            }
        }
        binding.itemRecycler.adapter = itemsAdapter
        getData()
    }

    private fun getData() {
        showProgress("加载中", "", true)
        lifecycleScope.launch(CoroutineExceptionHandler { dismissProgress() }) {
            val response = withContext(Dispatchers.IO) {
                ClassApi.service.getClassAttnList(type = type.toString())
            }
            dismissProgress()
            if (response.isSuccessful) {
                itemsAdapter.notifyChanged(response.data)
            }
        }
    }

}